package fr.jade.fraclite.orb;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import fr.jade.fraclite.orb.io.messages.RpcRequest;

public class InvokeTask implements Runnable {

  protected ObjectOutputStream oos;
  protected Object target;
  protected RpcRequest id;
  
  public InvokeTask(Object target, ObjectOutputStream oos, RpcRequest id){
    this.target = target;
    this.oos = oos;
    this.id = id;
  }
  
  @Override
  public void run() {

    Object result = null;
    try{
      Method m = target.getClass().getDeclaredMethod(id.getMethodName(), id.getArgTypes());
      System.out.println("[skel]"+id.getItfId()+" "+m.toGenericString());
      result = m.invoke(target, id.getArgs());
    }catch(InvocationTargetException e){
      result = e;
    }catch(Exception e){
      e.printStackTrace();
    }
    try {
      oos.writeObject(result);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
